package com.ruoyi.service.impl;

import com.ruoyi.entity.Classes;
import com.ruoyi.mapper.EsMapper;
import com.ruoyi.service.EsService;
import com.ruoyi.utils.IdWorker;
import com.ruoyi.utils.Result;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EsServiceImpl implements EsService {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Resource
    private IdWorker idWorker;

    @Resource
    private EsMapper esMapper;

    public Result save() {


        List<Classes> list=esMapper.esList();
        for (Classes classes : list) {
            IndexRequest indexRequest = new IndexRequest("classes", "classes");
            indexRequest.id(idWorker.nextId()+"");
            Map map=new HashMap<>();
            map.put("classesName",classes.getClassesName());
            map.put("classesVideo",classes.getClassesVideo());
            map.put("status",classes.getStatus());
            indexRequest.source(map);
            try {
                restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        }
        return Result.success();
    }

    public Result list(Classes classes){

        SearchRequest searchRequest = new SearchRequest("classes");
        searchRequest.types("classes");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (classes.getClassesName()!=null && !classes.getClassesName().equals("")){
            MultiMatchQueryBuilder classesName = QueryBuilders.multiMatchQuery(classes.getClassesName(),"classesName");
            boolQueryBuilder.must(classesName);
        }

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("classesName");
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);

        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = null;
        try {
            search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SearchHits hits = search.getHits();

        SearchHit[] hitsHits = hits.getHits();
        List list=new ArrayList<>();
        for (SearchHit hitsHit : hitsHits) {
            String id = hitsHit.getId();
            Map<String, Object> sourceAsMap = hitsHit.getSourceAsMap();
            sourceAsMap.put("id", id);
            Map<String, HighlightField> highlightFields =
                    hitsHit.getHighlightFields();
            if (highlightFields.size()>0){
                HighlightField field = highlightFields.get("classesName");
                Text fragment = field.getFragments()[0];
                String classesName = fragment.toString();
                sourceAsMap.put("classesName",classesName);
            }
            list.add(sourceAsMap);

        }


        return Result.success(list);
    }

    public Result updateStatus(Integer id,Integer status) {
        if (id!=null && status==1){
            esMapper.updateStatus(id,status);

            return Result.success("审核通过");
        }

        if (id!=null && status==2){
            esMapper.updateStatus(id,status);

            return Result.success("审核不通过");
        }
        return Result.success();
    }
}
